package com.stoneapple.timesheet.ProfileServlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.stoneapple.timesheet.dao.ProfileDao;
import com.stoneapple.timesheet.entity.Profile;

@SuppressWarnings("serial")
public class ProfileServlet extends HttpServlet{

		public void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws IOException, ServletException {
			
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			
			if (user == null) {
				String loginUrl = userService.createLoginURL(req.getRequestURI());
				resp.sendRedirect(loginUrl);
				
			} else {
				
				
				ProfileDao profileDao = ProfileDao.getInstance();
				List<Profile> profile = profileDao.getProfileListByUserId(user.getUserId());
				
				req.setAttribute("profiles", profile);

				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/profilemain.jsp");
				dispatcher.forward(req, resp);
			}
		}

	}


